import requests

api_url = "http://127.0.0.1/v1/chat-messages"
api_key = "app-PMFC3fiTtJKuLnkVBkrGqxKy"

# 设置请求头
headers = {
    "Content-Type": "application/json",
    "Authorization": f"Bearer {api_key}"  # 如果接口需要 Bearer Token 认证
}
# # 读取D:\subtitle\bring_goods\4\ I Bought A FAKE James Charles Palette .txt
# with open(r"D:\subtitle\bring_goods\4\ I Bought A FAKE James Charles Palette .txt", "r", encoding="utf-8") as file:
#     text = file.read()
#     print(text)
def get_text(instruction,text):
    # 要发送的消息内容
    data_raw = {
        "inputs": {},
        "query": f"{instruction}:{text}.",
        "response_mode": "blocking",  # 使用阻塞模式（假设 API 支持此模式）
        "conversation_id": "",
        "user": "yaaakaaang",
        "files": [
            {
                "type": "image",
                "transfer_method": "remote_url",
                "url": "https://cloud.dify.ai/logo/logo-site.png"
            }
        ]
    }

    try:
        # 发送 POST 请求，阻塞等待完整响应
        response = requests.post(api_url, headers=headers, json=data_raw)

        # 检查 HTTP 响应码
        response.raise_for_status()

        # 解析 JSON 响应
        response_json = response.json()
        answer = response_json.get("answer", "无答案")
        text = answer.split(">")[2]
        # print(text)
        return text
    except requests.exceptions.HTTPError as http_err:
        print(f"HTTP 错误发生: {http_err}")
    except Exception as err:
        print(f"其他错误发生: {err}")
